package com.example.demo.service;

import com.example.demo.mapper.CommentMapper;
import com.example.demo.model.CommentInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CommentService {
    
    private static final Logger log = LoggerFactory.getLogger(CommentService.class);
    
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 创建初始评论记录
     */
    @Transactional
    public boolean createInitialComment(String orderNo, Integer buyerId, Integer productId, Integer sellerId) {
        try {
            CommentInfo commentInfo = new CommentInfo();
            commentInfo.setOrderNo(orderNo);
            commentInfo.setBuyerId(buyerId);
            commentInfo.setProductId(productId);
            commentInfo.setSellerId(sellerId);
            
            return commentMapper.createInitialComment(commentInfo) > 0;
        } catch (Exception e) {
            log.error("创建初始评论记录失败: orderNo={}", orderNo, e);
            throw e;
        }
    }

    /**
     * 买家提交评论
     */
    @Transactional
    public boolean submitBuyerComment(String orderNo, Integer buyerId, String content, Integer rating) {
        try {
            // 1. 获取评论记录
            CommentInfo comment = commentMapper.getByOrderNo(orderNo);
            if (comment == null || !comment.getBuyerId().equals(buyerId)) {
                log.warn("评论不存在或无权限: orderNo={}, buyerId={}", orderNo, buyerId);
                return false;
            }

            // 2. 检查评论状态
            if ("已评价".equals(comment.getBuyerCommentStatus())) {
                log.warn("买家已评价过: orderNo={}", orderNo);
                return false;
            }

            // 3. 更新评论
            comment.setContent(content);
            comment.setRating(rating);
            
            return commentMapper.updateBuyerComment(comment) > 0;
        } catch (Exception e) {
            log.error("买家提交评论失败: orderNo={}", orderNo, e);
            throw e;
        }
    }

    /**
     * 卖家提交评论
     */
    @Transactional
    public boolean submitSellerComment(String orderNo, Integer sellerId, String content, Integer rating) {
        try {
            // 1. 获取评论记录
            CommentInfo comment = commentMapper.getByOrderNo(orderNo);
            if (comment == null || !comment.getSellerId().equals(sellerId)) {
                log.warn("评论不存在或无权限: orderNo={}, sellerId={}", orderNo, sellerId);
                return false;
            }

            // 2. 检查评论状态
            if ("已评价".equals(comment.getSellerCommentStatus())) {
                log.warn("卖家已评价过: orderNo={}", orderNo);
                return false;
            }

            // 3. 更新评论
            comment.setSellerContent(content);
            comment.setSellerRating(rating);
            
            return commentMapper.updateSellerComment(comment) > 0;
        } catch (Exception e) {
            log.error("卖家提交评论失败: orderNo={}", orderNo, e);
            throw e;
        }
    }

    /**
     * 获取评论信息
     */
    public CommentInfo getComment(String orderNo, Integer userId) {
        try {
            CommentInfo comment = commentMapper.getByOrderNo(orderNo);
            if (comment == null) {
                return null;
            }

            // 验证权限（买家或卖家）
            if (!comment.getBuyerId().equals(userId) && !comment.getSellerId().equals(userId)) {
                log.warn("无权限查看评论: orderNo={}, userId={}", orderNo, userId);
                return null;
            }

            return comment;
        } catch (Exception e) {
            log.error("获取评论失败: orderNo={}", orderNo, e);
            throw e;
        }
    }
}
